package top.greatxiaozou.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("manager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(securityManager);

        Map<String,String> filter = new HashMap<>();


        factoryBean.setFilterChainDefinitionMap(filter);


        factoryBean.setLoginUrl("*/login");
        return factoryBean;
    }



    //创建管理器
    @Bean(name = "manager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm,@Qualifier("adminRealm") AdminRealm adminRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();

        securityManager.setRealm(userRealm);
        securityManager.setRealm(adminRealm);
        return securityManager;
    }




    //创建真实对象
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

    @Bean
    public AdminRealm adminRealm(){
        return new AdminRealm();
    }
}
